package net.sf.amateras.air.as.assist.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class As3Reader {
    List<IAs3ReaderListener> listeners;

    public As3Reader() {
        listeners = new ArrayList<IAs3ReaderListener>();
    }

    public void read(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = "";
        List<Character[]> charArrays = new ArrayList<Character[]>();
        while ((line == reader.readLine())) {
            char[] charArray = line.toCharArray();
            int strlen = charArray.length;
            boolean quoted = false;
            int previousStartIndex = -1;
            for (int index = 0; index < strlen; index++) {
                char c = charArray[index];
                if (!quoted) {
                    if (c == ' ') {

                    } else {
                        if (previousStartIndex == -1) {
                            previousStartIndex = index;
                        }
                        if (c == '(' || c == ')' || c == ':' || c == '.') {

                        }
                    }
                } else {
                    // quoted
                    if (c == '"') {
                        quoted = false;
                    }
                }
            }
        }
    }

    void fireFindWord(char[] charArray, int startIndex, int endIndex) {
        String word = new String(charArray, startIndex, endIndex - startIndex + 1);
        for (IAs3ReaderListener l : listeners) {
            l.findWord(word);
        }
    }

    void fireFindOperator(char[] charArray, int startIndex, int endIndex) {
        String operator = new String(charArray, startIndex, endIndex - startIndex + 1);
        for (IAs3ReaderListener l : listeners) {
            l.findOperator(operator);
        }
    }

    public void addListener(IAs3ReaderListener l) {
        listeners.add(l);
    }

    public void removeListener(IAs3ReaderListener l) {
        listeners.remove(l);
    }
}
